package com.myx.dao.user;

import com.myx.pojo.User;
import com.myx.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    public JdbcUtil jdbcUtil=new JdbcUtil();

    @Override
    public boolean addUser(User user) {
        jdbcUtil.getConnection();
        String sql="insert into news_user(`username`,`password`,`email`,`userType`)values (?,?,?,?);";
        Object[] params={user.getUsername(),user.getPassword(),user.getEmail(),user.getUserType()};
        return jdbcUtil.addDeleteModify(sql, params);
    }

    @Override
    public int verifyOnlyUsername(String username) {
        jdbcUtil.getConnection();
        String sql="select count(1) from news_user where username=?";
        Object [] params={username};
        ResultSet resultSet=jdbcUtil.getObjectByParams(sql,params);
        int count=0;
        try {
            while (resultSet.next()){
                count=resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return count;
    }

    //  通过用户名查询用户信息
    public User getUserByUsername(String username){
//        jdbcUtil.getConnection();
        String sql="select * from news_user where username=?";
        Object[] params={username};
        ResultSet resultSet= jdbcUtil.getObjectByParams(sql,params);
        try{
            while (resultSet.next()){
                Integer id=resultSet.getInt("id");
                String username1= resultSet.getString("username");
                String password= resultSet.getString("password");
                String email= resultSet.getString("email");
                Integer userType= resultSet.getInt("userType");
                User user=new User();
                user.setId(id);
                user.setUsername(username1);
                user.setPassword(password);
                user.setEmail(email);
                user.setUserType(userType);
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return null;
    }

    @Override
    public User getUserById(Integer id) {

        String sql="select *from news_user where id=?";
        Object[] params={id};
        ResultSet resultSet = this.jdbcUtil.getObjectByParams(sql, params);
        User user = new User();

        try {
            while (resultSet.next()){
                Integer id2=resultSet.getInt("id");
                String username1= resultSet.getString("username");
                String password= resultSet.getString("password");
                String email= resultSet.getString("email");
                Integer userType= resultSet.getInt("userType");
                user.setId(id2);
                user.setUsername(username1);
                user.setPassword(password);
                user.setEmail(email);
                user.setUserType(userType);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.jdbcUtil.closeResource();
        }
        return user;
    }

    public boolean add(User user){
        String sql = "insert into news_user(`username`,`password`,`email`,`userType`) value (?,?,?,?);";
        Object[] params = {user.getUsername(), user.getPassword(), user.getEmail(), user.getUserType()};
        return jdbcUtil.addDeleteModify(sql, params);

    }
}
